package com.skyolder.crutch.word.token.raw;

import com.skyolder.crutch.error.GrammarError;
import com.skyolder.crutch.word.raw.BufferCharPiper;
import com.skyolder.crutch.word.raw.BufferCharQueue;
import com.skyolder.crutch.word.types.RawTokenType;

public class StringTokenCreator implements TokenCreator {

	@Override
	public RawToken tryCreateToken(BufferCharQueue bufferCharQueue) throws GrammarError {
		char c = bufferCharQueue.peekChar();
		if(c!='"'){
			return null;
		}
		bufferCharQueue.nextChar();
		StringBuilder stringBuilder=new StringBuilder();
		boolean shift=false;
		while(true){
			char cc = bufferCharQueue.peekChar();
			if(cc=='\\'){
				shift=true;
			}else if(cc=='"'&&shift==false){
				break;
			}else{
				shift=false;
			}
			stringBuilder.append(cc);
		}
		return new  RawToken(stringBuilder.toString(), RawTokenType.STRING);
	}

}
